package com.boesch.electroOffice.presentation.invoice;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import org.springframework.stereotype.Controller;

import com.boesch.electroOffice.model.enums.SessionParameter;
import com.boesch.electroOffice.model.enums.SubfunctionIdentifier;
import com.boesch.electroOffice.utils.ConfigurationUtils;
import com.boesch.electroOffice.utils.PortletUtils;
import com.boesch.electroOffice.utils.RoleUtils;
import com.liferay.portal.model.Role;

/**
 * This class is used for the navigation in invoice portlet
 * 
 * @author Ralph Koster
 *
 */
@Controller
@ManagedBean
@SessionScoped
public class InvoiceNavigationController implements Serializable {

	private static final long serialVersionUID = -4545120044790353052L;

	private String navigationPath = "/jsf/invoice/invoice_list.xhtml";

	public String getNavigationPath() {
		return navigationPath;
	}
	
	public void setNavigationPath(ActionEvent event) {
		String key = (String)event.getComponent().getAttributes().get("key");
		if(key.equals(SubfunctionIdentifier.INVOICE_ABRIDGEMENT_REPORT.getValue())) {
			PortletUtils.setSessionAttribute(SessionParameter.SUBFUNCTION_KEY.getValue(), key);
			navigationPath = "/jsf/common/pdf_reader.xhtml";
		} else if(key.equals(SubfunctionIdentifier.OPEN_INVOICES_REPORT.getValue())) {
			PortletUtils.setSessionAttribute(SessionParameter.SUBFUNCTION_KEY.getValue(), key);
			navigationPath = "/jsf/common/pdf_reader.xhtml";
		} else if(key.equals(SubfunctionIdentifier.PAYED_INVOICES_REPORT.getValue())) {
			PortletUtils.setSessionAttribute(SessionParameter.SUBFUNCTION_KEY.getValue(), key);
			navigationPath = "/jsf/common/pdf_reader.xhtml";
		} else {
			navigationPath = "/jsf/invoice/invoice_list.xhtml";
		}
	}
	
	public void setNavigationPath(String navigationPath) {
		this.navigationPath = navigationPath;
	}
	
	public boolean hasPermission(String key) {
		boolean returnValue = false;
		List<Role> roles = RoleUtils.getCurrentUsersRoles();
		for(Role role : roles) {
			if(ConfigurationUtils.getRoleSubfunctionPreference(role.getName(), key)) {
				returnValue = true;
				break;
			}
		}
		return returnValue;
	}
	
	public String[] getMainLinkKeys() {
		return InvoiceSubfunctionUtils.getMainLinkKeys();
	}
	
	public String[] getReportLinkKeys() {
		return InvoiceSubfunctionUtils.getReportLinkKeys();
	}
}
